package com.tjp.eight.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2017/3/21.
 */

public class FileUtils {

    /**
     * 通过URI获取图片PATH
     *
     * @param uri
     * @return
     */
    public static String imgPath(Activity activity, Uri uri) throws IllegalAccessException {
        String[] img = {MediaStore.Images.Media.DATA};
        Cursor cursorImg = activity.getContentResolver().query(uri, img, null, null, null);//activity.managedQuery(uri, img, null, null, null);
        if (cursorImg == null) return "";
        int index = cursorImg.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursorImg.moveToFirst();
        return cursorImg.getString(index);
    }

    public static String saveBitmapFile(Activity activity, Bitmap bitmap, String saveName) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                String[] per = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
                ActivityCompat.requestPermissions(activity, per, 1111);
            }
        }
        File file = new File(Environment.getExternalStorageDirectory()
                .getAbsoluteFile() + "/" + appName_temp + "/", saveName);//将要保存图片的路径
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
            return file.getPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String appName_temp = "八秒视频_temp";

    public static File saveBitmap(Activity activity, File file) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                String[] per = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
                ActivityCompat.requestPermissions(activity, per, 1111);
            }

        }
        File f = new File(Environment.getExternalStorageDirectory()
                .getAbsoluteFile() + "/" + appName_temp + "/", file.getName());
        if (!f.mkdir()) {
            f.mkdirs();
        }
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            BitmapFactory.Options os = new BitmapFactory.Options();
            os.inJustDecodeBounds = false;
            os.inDither = false;
            os.inPurgeable = true;
            os.inTempStorage = new byte[12 * 1024];
            os.inSampleSize = 10;
            Bitmap bt = BitmapFactory.decodeFile(file.getPath(), os);
            bt.compress(Bitmap.CompressFormat.JPEG, 80, out);
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return f;
    }

    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);// 再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

    // 删除文件夹
    // param folderPath 文件夹完整绝对路径

    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // 删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete(); // 删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
